home *** CD-ROM | disk | FTP | other *** search
/ Aminet 24 / Aminet 24 (1998)(GTI - Schatztruhe)[!][Apr 1998].iso / Aminet / util / wb / wbstars2.lha / wbstars2.0 / source / umain.c < prev    next >
C/C++ Source or Header  |  1991-08-18  |  5KB  |  183 lines

  1. /*      _main.c         Copyright (C) 1985  Lattice, Inc.       */
  2.  
  3. #include <stdio.h>
  4. #include <fcntl.h>
  5. #include <ios1.h>
  6. #include <string.h>
  7. #include <stdlib.h>
  8. #include <workbench/startup.h>
  9. #include <libraries/dos.h>
  10. #include <libraries/dosextens.h>
  11. #include <proto/dos.h>
  12. #include <proto/exec.h>
  13.  
  14. #define MAXARG 32
  15. #define QUOTE  '"'
  16. #define ESCAPE '*'
  17. #define ESC '\027'
  18. #define NL '\n'
  19.  
  20. #define isspace(c)      ((c == ' ')||(c == '\t') || (c == '\n'))
  21.  
  22. #ifndef TINY
  23. extern int _fmode,_iomode;
  24. extern int (*_ONBREAK)();
  25. extern int CXBRK();
  26. #endif
  27.  
  28. extern struct UFB _ufbs[];
  29. extern void main(int, char**);
  30. static int argc;                       /* arg count */
  31. static char **targv, *argv[MAXARG];   /* arg pointers */
  32. static void badarg(char *program);
  33.  
  34. #define MAXWINDOW 40
  35. extern struct WBStartup *WBenchMsg;
  36. /**
  37. *
  38. * name         _main - process command line, open files, and call "main"
  39. *
  40. * synopsis     _main(line);
  41. *              char *line;     ptr to command line that caused execution
  42. *
  43. * description   This function performs the standard pre-processing for
  44. *               the main module of a C program.  It accepts a command
  45. *               line of the form
  46. *
  47. *                       pgmname arg1 arg2 ...
  48. *
  49. *               and builds a list of pointers to each argument.  The first
  50. *               pointer is to the program name.  For some environments, the
  51. *               standard I/O files are also opened, using file names that
  52. *               were set up by the OS interface module XCMAIN.
  53. *
  54. **/
  55. void _main(line)
  56. register char *line;
  57. {
  58.    register char **pargv;
  59.    register int x;
  60.    struct Process *process;
  61.    struct FileHandle *handle;
  62.    static char window[MAXWINDOW+18];
  63.    char *argbuf;
  64.  
  65. /*
  66. *
  67. * Build argument pointer list
  68. *
  69. */
  70.    
  71.    while (argc < MAXARG)
  72.    {
  73.         while (isspace(*line))  line++;
  74.         if (*line == '\0')      break;
  75.         pargv = &argv[argc++];
  76.         if (*line == QUOTE)
  77.         {
  78.             argbuf = *pargv = ++line;  /* ptr inside quoted string */
  79.             while (*line != QUOTE && *line != 0)
  80.             {
  81.                if (*line == ESCAPE)
  82.                {
  83.                   line++;
  84.                   switch (*line)
  85.                   {
  86.                      case '0':
  87.                         *argbuf = 0;
  88.                         goto linedone;
  89.                      case 'E':
  90.                         *argbuf++ = ESC;
  91.                         break;
  92.                      case 'N':
  93.                         *argbuf++ = NL;
  94.                         break;
  95.                      default:
  96.                         *argbuf++ = *line;
  97.                   }
  98.                   line++;
  99.                }
  100.                else
  101.                {
  102.                  *argbuf++ = *line++;
  103.                }
  104.             }
  105.             line++;
  106.             *argbuf++ = '\0'; /* terminate arg */
  107.         }
  108.         else            /* non-quoted arg */
  109.         {       
  110.             *pargv = line;
  111.             while ((*line != '\0') && (!isspace(*line))) line++;
  112.             if (*line == '\0')  break;
  113.             else                *line++ = '\0';  /* terminate arg */
  114.         }
  115.    }  /* while */
  116.  
  117. linedone:
  118.  
  119.    targv = (argc == 0) ? (char **)WBenchMsg : (char **)&argv[0];
  120.  
  121.  
  122. /*
  123. *
  124. * Open standard files
  125. *
  126. */
  127. #ifndef TINY
  128.  
  129.    if (argc == 0)          /* running under workbench      */
  130.    {
  131.         strcpy(window, "con:10/10/320/80/");
  132.         strncat(window, WBenchMsg->sm_ArgList->wa_Name,MAXWINDOW);
  133.         _ufbs[0].ufbfh = Open(window,MODE_NEWFILE);
  134.         _ufbs[1].ufbfh = _ufbs[0].ufbfh;
  135.         _ufbs[1].ufbflg = UFB_NC;
  136.         _ufbs[2].ufbfh = _ufbs[0].ufbfh;
  137.         _ufbs[2].ufbflg = UFB_NC;
  138.         handle = (struct FileHandle *)(_ufbs[0].ufbfh << 2);
  139.         process = (struct Process *)FindTask(0);
  140.         process->pr_ConsoleTask = (APTR)handle->fh_Type;
  141.         x = 0;
  142.    }
  143.    else                    /* running under CLI            */
  144.    {
  145.         _ufbs[0].ufbfh = Input();
  146.         _ufbs[1].ufbfh = Output();
  147.         if ((_ufbs[2].ufbfh = Open("*", MODE_OLDFILE)) == NULL)
  148.              _ufbs[2].ufbfh = Open("NIL:", MODE_OLDFILE);
  149.         x = UFB_NC;                     /* do not close CLI defaults    */
  150.    }
  151.  
  152.    _ufbs[0].ufbflg |= UFB_RA | O_RAW | x;
  153.    _ufbs[1].ufbflg |= UFB_WA | O_RAW | x;
  154.    _ufbs[2].ufbflg |= UFB_RA | UFB_WA | O_RAW;
  155.  
  156.    x = (_fmode) ? 0 : _IOXLAT;
  157.    stdin->_file = 0;
  158.    stdin->_flag = _IOREAD | x;   
  159.    stdout->_file = 1;
  160.    stdout->_flag = _IOWRT | x;
  161.    stderr->_file = 2;
  162.    stderr->_flag = _IORW | x;
  163.  
  164. /*      establish control-c handler */
  165.  
  166. _ONBREAK = CXBRK;
  167.  
  168. #endif
  169.  
  170. /*
  171. *
  172. * Call user's main program
  173. *
  174. */
  175.  
  176.    main(argc,targv);              /* call main function */
  177. #ifndef TINY
  178.    exit(0);
  179. #else
  180.    _exit(0);
  181. #endif
  182. }
  183.